package com.hujiang.hummer.xmpp;

public class JID implements Comparable<JID> {

	private final BareJID _bareJid;
	private final String _resource;
	private final String _to_string;
	private final int _hashCode;
	
	private JID(BareJID bJid, String resource) {

		this._bareJid = bJid;
		this._resource = resource;
		this._to_string = (resource != null || resource.length() > 0)
				? bJid.toString() + "/" + resource : bJid.toString();
		this._hashCode = this._to_string.hashCode();
	}
	
	public static JID newInstance(String jid) {
		
		String[] strArr = BareJID.parseJID(jid);
		
		BareJID bJid = BareJID.newInstance(strArr[0], strArr[1]);
		
		return new JID(bJid, strArr[3]);
	}
	
	public static JID newInstance(BareJID bJid) {
		
		return new JID(bJid, null);
	}
	
	@Override
	public String toString() {
		
		return this._to_string;
	}
	
	@Override
	public boolean equals(Object obj) {

		boolean result = false;
		
		if (obj instanceof JID) {
			
			JID jid = (JID)obj;
			result = this._bareJid.equals(jid._bareJid)
					&& (this._resource == null || this._resource.equals(jid._resource));
		}
		
		return result;
	}
	
	@Override
	public int compareTo(JID other) {
		
		return this._to_string.compareTo(other._to_string);
	}

	@Override
	public int hashCode() {

		return this._hashCode;
	}
	
	public String get_resource() {
		return _resource;
	}
	
	public BareJID get_bareJid() {
		return _bareJid;
	}
	
	public String get_name() {
		return this._bareJid.get_name();
	}
	
	public String get_domain() {
		return this._bareJid.get_domain();
	}
}
